﻿using UnityEngine;

namespace SillyCube
{
    public class CubeContainer : MonoBehaviour
    {
        [SerializeField] private GameObject cubePrefab;

        private CubeController _currentCubeController;

        public CubeController CurrentCubeController => _currentCubeController;
        
        
        public void SpawnCube(Vector2 position, bool destroyLoaded = true)
        {
            if (destroyLoaded && _currentCubeController != null)
            {
                Destroy(_currentCubeController.gameObject);
            }
            
            var go = Instantiate(cubePrefab, transform);
            go.transform.position = position;
            
            _currentCubeController = go.GetComponent<CubeController>();
        }
    }
}